#define  _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

void SLInit(SL* ptr)
{
	assert(ptr);
	ptr->a = NULL;
	ptr->top = 0;
	ptr->capacity = 0;
}

void SLDestroy(SL* ptr)
{
	assert(ptr);
	free(ptr->a);
	ptr->a=NULL;
	ptr->top = 0;
	ptr->capacity = 0;
}

void SLPush(SL* ptr,SLdatatype x)
{
	if (ptr->top == ptr->capacity)
	{
		int newCapacity = ptr->capacity == 0 ? 4 : ptr->capacity * 2;
		SLdatatype* tmp= (SLdatatype*)realloc(ptr->a, newCapacity*sizeof(SLdatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ptr->a = tmp;
		ptr->capacity = newCapacity;
	}
	ptr->a[ptr->top] = x;
	ptr->top++;
}

void SLPop(SL* ptr)
{
	assert(ptr);
	assert(!SLEmpty(ptr));
	ptr->top--;
}

SLdatatype SLTop(SL* ptr)
{
	assert(ptr);
	assert(!SLEmpty(ptr));
	return ptr->a[ptr->top - 1];
}

bool SLEmpty(SL* ptr)
{
	assert(ptr);
	return ptr->top == 0;
}